package com.ethan.minimalist.ui.mine

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.cxz.kotlin.baselibs.base.BaseFragment
import com.cxz.kotlin.baselibs.ext.setSingleClickListener
import com.cxz.kotlin.baselibs.ext.showToast
import com.ethan.minimalist.R
import com.ethan.minimalist.bean.UserBean
import com.ethan.minimalist.constant.Constant
import com.ethan.minimalist.ui.login.LoginActivity
import com.ethan.minimalist.ui.mine.withdraw.WalletFirstStepActivity
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.fragment_mine.*

/**
 *@author ethan_
 *date 2020/3/15 20:30
 *desc:
 */
class MineFragment : BaseFragment() {

    private var mUser: UserBean? = null

    override fun attachLayoutRes(): Int {
        return R.layout.fragment_mine
    }

    override fun initView(view: View) {
        addListener()
    }

    private fun addListener() {
        iv_phone?.setOnClickListener {
            activity?.startActivity(Intent(activity, LoginActivity::class.java))
        }

        cl_user.setOnClickListener {
            activity?.startActivity(Intent(activity, UpdateUserActivity::class.java))
        }

        ll_coins.setSingleClickListener {
            activity?.startActivity(Intent(activity, WalletFirstStepActivity::class.java))
        }


        iv_wechat.setOnClickListener {
            showMsg()
        }
        iv_qq.setOnClickListener {
            showMsg()
        }
        iv_weibo.setOnClickListener {
            showMsg()
        }
        ll_task_money.setOnClickListener {
            showMsg()
        }

        ll_first_base_func.setOnClickListener {
            showMsg()
        }
        ll_second_base_func.setOnClickListener {
            showMsg()
        }
        tv_help.setOnClickListener {
            showMsg()
        }
        tv_setting.setOnClickListener {
            showMsg()
        }
    }

    private fun showMsg() {
        showToast("请升级版本")
    }

    override fun lazyLoad() {

    }


    override fun onResume() {
        super.onResume()
        mUser =
            Gson().fromJson(MMKV.defaultMMKV().decodeString(Constant.USER), UserBean::class.java)
        if (mUser != null) {
            updateView()
        } else {
            cl_user.visibility = View.GONE
            ll_coins.visibility = View.GONE

            ll_login.visibility = View.VISIBLE
        }
    }

    private fun updateView(): Unit {
        cl_user.visibility = View.VISIBLE
        ll_coins.visibility = View.VISIBLE
        ll_login.visibility = View.GONE

        Glide.with(this).load(mUser?.avatar).into(iv_avatar)
        tv_user_num.text = mUser?.nickName ?: mUser?.userName ?: mUser?.mobile
        if (TextUtils.isEmpty(mUser!!.userName) || TextUtils.isEmpty(mUser!!.nickName)) {
            tv_user_name.text = "用户名：无"
        } else {
            tv_user_name.text = "用户名：${mUser?.userName ?: mUser?.nickName ?: mUser?.mobile}"
        }

        tv_balance.text = mUser?.balance
        tv_yesterday_coin.text = mUser?.yestodayMoney
        tv_today_coin.text = mUser?.coins
    }

    companion object {
        fun getInstance(title: String): MineFragment {
            val fragment = MineFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }
}